/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.ieslaferreria.creditsintesis.controllers;

import edu.ieslaferreria.creditsintesis.logicgame.*;
import edu.ieslaferreria.creditsintesis.logicgame.actions.RmiEvent;
import edu.ieslaferreria.creditsintesis.rmi.GameEvent;
import edu.ieslaferreria.creditsintesis.rmi.Pont;
import java.net.ConnectException;
import java.rmi.RemoteException;


 
/**
 * controla los movimientos de los objetos y actualiza las nuevas posiciones
 * siempre que se pueda, además de actulizar los objetos RMI.
 * @author Rubén
 */
public class InteractionController {

    ActionStates acState;
    Pont pont;

    public InteractionController(ActionStates acState, Pont pont){
        this.acState = acState;
        this.pont=pont;
    }

    public void update(float time) throws RemoteException,ConnectException{
        ObjectWithStates obj = null;
        for(int elems = this.acState.getObjectsToValidateState().size();
                    !this.acState.getObjectsToValidateState().isEmpty()
                    && elems>0; elems--){
        //while(!this.acState.getObjectsToValidateState().isEmpty()){
            obj = this.acState.getObjectsToValidateState().poll();
                RmiEvent rm=obj.updateAction(time);
            if (rm!=null){
                System.out.println("Actualizcion Posicion Objecto " + pont.getPontName());
                rm.setEventGeneratorPontName(pont.getPontName());
                pont.send(rm);
                System.out.println("Actualización: x:"+obj.getLocalTranslation().x +
                               " y:"+obj.getLocalTranslation().y +
                               " z:"+obj.getLocalTranslation().z);
            }
        }

    }
}
